<?php
require_once '../db_connection.php';
class GiangVien {

	public function logIn($tendangnhap, $matkhau){
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("select * from `TAIKHOAN` where TENDANGNHAP='" .$tendangnhap."' and MATKHAU='" .$matkhau."'
				and QUYEN=2");
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
	public function getNamHoc($magv){
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("SELECT NHOC FROM GIANGDAY where MASOGV =" .$magv. " group by NHOC;");
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
	public function getDanhSachHienTai(){
		$today = getdate();
		//echo $today;
		$currentDate = $today["mday"]. "-" .$today["mon"]. "-" .$today["year"];
		echo $currentDate;
	}
	public function getDanhSachHP($nienkhoa, $hocki, $masogv){
		$data = new DatabaseConnect();
		$data->Open();
		
		/* $data->setSql("SELECT MAHP, KIHIEU FROM SQA.GIANGDAY where MASOGV = '".$masogv."' and HKI = '".$hocki."' and NHOC = '".$nienkhoa."'"); */
		$data->setSql("select a.MAHP, TENHP, a.KIHIEU, THU, TIET, PHONG, a.HKI, a.NHOC from TKB a join GIANGDAY b on a.MAHP = b.MAHP
 			and a.HKI = b.HKI where b.HKI = '".$hocki."' and b.NHOC = '".$nienkhoa."' and b.MASOGV = '".$masogv."'
			group by a.KIHIEU;");
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
	
	public function getTKB($magv, $mahp, $nhomhp, $hk, $nienkhoa) {
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("select THU, TIET FROM TKB a join GIANGDAY b on a.MAHP = b.MAHP and a.KIHIEU = b.KIHIEU 
			where a.MAHP ='".$mahp."' and a.KIHIEU ='" .$nhomhp."' and a.HKI = ".$hk." and a.NHOC = '".$nienkhoa."'
			and b.MASOGV = '".$magv."';"
		);
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
	
	/**
	 * lay ra ten giao vien khi dang nhap vao he thong
	 */
	public function getTenGV($taikhoan){
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("SELECT HOTENGV FROM SQA.GIANGVIEN a join TAIKHOAN b on a.MASOGV = b.MASOGV
			WHERE b.MASOGV = '".$taikhoan."';");
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
	
	/**
	 * lay noi dung nhat ki
	 */
	
	public function getNhatKi($mahocphan, $nhomhp, $hocki, $namhoc, $magv){
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("select THOIGIAN, NOIDUNG_GV from SONHATKI where MAHP='".$mahocphan."' 
				and KIHIEU='".$nhomhp."' and HKI='".$hocki."' and NHOC='".$namhoc."' and MASOGV='".$magv."'");
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
	
	/**
	 * ghi nhat ki
	 */
	public function setNhatKi($mahocphan, $nhomhp, $hocki, $namhoc, $magv, $noidung, $thoigian){
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("insert into SONHATKI(MAHP, KIHIEU, HKI, NHOC, MASOGV, NOIDUNG_GV, TRANGTHAI, THOIGIAN)
			values('".$mahocphan."', '".$nhomhp."', '".$hocki."', '".$namhoc."', '".$magv."',
				'".$noidung."', '1', '".$thoigian."');");
		$data->ExecuteQuery();
		$data->Close();
	}
	/**
	 * Cap nhat lai phan nhat ki cua giang vien
	 * khi da co sinh vien ghi truoc noi dung
	 */
	public function updateNhatKi($mahocphan, $nhomhp, $hocki, $namhoc, $magv, $noidungnhap, $date){
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("update SONHATKI set MASOGV='".$magv."', NOIDUNG_GV='".$noidungnhap."', TRANGTHAI=0
			where MAHP='".$mahocphan."' and KIHIEU='".$nhomhp."' and NHOC='".$namhoc."' and THOIGIAN='".$date."'");
		$data->ExecuteQuery();
		$data->Close();
	}
	
	/**
	 * Lay trang thai cua nhat ki
	 */
	public function getTrangThai($mahocphan, $nhomhocphan, $hocki, $namhoc, $magv, $date){
		//echo "aaaaaaaaaaaaaaaaaaa";
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("select TRANGTHAI FROM SONHATKI where MAHP='".$mahocphan."' and KIHIEU='".$nhomhocphan."'
				and HKI='".$hocki."' and NHOC='".$namhoc."'
 				and MASOGV='".$magv."' and THOIGIAN='".$date."'");
		/* echo "select TRANGTHAI FROM SONHATKI where MAHP='".$mahocphan."' and KIHIEU='".$nhomhocphan."'
				and HKI='".$hocki."' and NHOC='".$namhoc."'
 				and MASOGV='".$magv."' and THOIGIAN='".$date."'"; */
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
	/**
	 * Dat ma truy cap cho hoc phan
	 */
	public function setMaTruyCap($mahocphan, $nhomhocphan, $hocki, $namhoc, $magv, $matruycap){
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("Update GIANGDAY set MATRUYCAP = '".$matruycap."' where MAHP='".$mahocphan."'
				 and KIHIEU='".$nhomhocphan."' and HKI=".$hocki." and NHOC='".$namhoc."' 
				 and MASOGV='".$magv."'");
		$data->ExecuteQuery();
		$data->Close();
	}
	
	/**
	 * Lay ma truy cap hoc phan
	 */
	public function getMaTruyCap($mahocphan, $nhomhocphan, $hocki, $namhoc, $magv){
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("select MATRUYCAP FROM GIANGDAY where MAHP='".$mahocphan."'
				 and KIHIEU='".$nhomhocphan."' and HKI=".$hocki." and NHOC='".$namhoc."'
				 and MASOGV='".$magv."'");
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
	
	/**
	 * Kiem tra xem ngay nhap nhat ki co ton tai hay chua
	 */
	public function testDate($date){
		$changeDate = date("Y-m-d", strtotime($date));
		//echo $changeDate;
		$test = false;
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("select THOIGIAN FROM SONHATKI;");
		$results = $data->LoadArray();
		$data->Close();
		foreach ($results as $result){
			//echo date("Y-m-d",strtotime($result[0]));
			if($changeDate == date("Y-m-d",strtotime($result[0]))){
				$test = true;
			}
		}
		return $test;
	}
	
	/**
	 * Kiem tra xem noi dung co duoc ghi hay chua
	 * echo $getnoidungSV[0][0];
	 */
	public function getNoiDungGV($mahocphan, $nhomhocphan, $hocki, $namhoc, $magv, $date){

		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("select NOIDUNG_GV FROM SONHATKI WHERE MAHP='".$mahocphan."' and KIHIEU='".$nhomhocphan."' and NHOC='".$namhoc."' and THOIGIAN='".$date."'
				and MASOGV='".$magv."'");
		/* echo "select NOIDUNG_GV FROM SONHATKI WHERE MAHP='".$mahocphan."' and KIHIEU='".$nhomhocphan."' and NHOC='".$namhoc."' 
				and THOIGIAN='".$date."' and MASOGV='".$magv."'";  */
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
	/**
	 * Lay noi dung sinh vien
	 */
	public function getNoiDungSV($mahocphan, $nhomhocphan, $hocki, $namhoc, $date){
		$data = new DatabaseConnect();
		$data->Open();
		$data->setSql("select NOIDUNG_SV FROM SONHATKI WHERE MAHP='".$mahocphan."' and KIHIEU='".$nhomhocphan."' 
				and NHOC='".$namhoc."' and HKI='".$hocki."' and THOIGIAN='".$date."' ");
		
		/* echo "select NOIDUNG_SV FROM SONHATKI WHERE MAHP='".$mahocphan."' and KIHIEU='".$nhomhocphan."' 
				and NHOC='".$namhoc."' and HKI='".$hocki."' and THOIGIAN='".$date."' "; */
		$result = $data->LoadArray();
		$data->Close();
		return $result;
	}
}
?>